package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YunLiuServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@ChooseProvider(channel = "yunliu")
public class YunLiuAdaptor implements IAdaptor {

	private static Logger log = Logger.getLogger(YunLiuAdaptor.class);

	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String pkgNo = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String tradeNo = orderBean.getIds();//订购流水号
	
		String result = YunLiuServiceImpl.service.order(pkgNo,phoneNo,tradeNo);
		log.info("between- 云流YunLiuService.order() 订购请求result<>:" + result);
		JSONObject resjson = JSONObject.fromObject(result);

	    int code = resjson.getInt("code");//状态码

		if ("1".equals(String.valueOf(code))) {
			int errorcode = resjson.getInt("errorcode");
			if ("200".equals(String.valueOf(errorcode))) {
				orderBean.setStatus("11");
				orderBean.setMessage("下单成功");
				orderBean.setErrorMsg("下单成功");
				JSONArray  message = resjson.getJSONArray("message");
				JSONObject res = JSONObject.fromObject(message.get(0));
				orderBean.setSuppOrderNo(String.valueOf(res.getInt("orderId")));
			}else {
				orderBean.setStatus("8");
				orderBean.setMessage("["+String.valueOf(errorcode)+"]"+parseResultCodeToMsg(String.valueOf(errorcode)));
				orderBean.setErrorMsg("["+String.valueOf(errorcode)+"]"+parseResultCodeToMsg(String.valueOf(errorcode)));
			}
			
		}else {

			orderBean.setStatus("8");
			orderBean.setMessage("訂購請求失敗");
			orderBean.setErrorMsg("訂購請求失敗");
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
	
		OrderBean orderBean = OrderInfoContext.getContext();
		String cstmOrderNo = orderBean.getIds();
        String suppOrderNo = orderBean.getSuppOrderNo();
        String phoneNo = orderBean.getPhoneNo();
		String result = YunLiuServiceImpl.service.orderStatus(cstmOrderNo,suppOrderNo,phoneNo);
		log.info("between- 云流YunLiuService.orderStatus() 查询响应response<> :" + result);
		

		JSONObject resjson = JSONObject.fromObject(result);

	    int code = resjson.getInt("code");//状态码

		if ("1".equals(String.valueOf(code))) {
			
			JSONArray  message = resjson.getJSONArray("message");
			JSONObject res = JSONObject.fromObject(message.get(0));
			String rechargeStatus = res.getString("rechargeStatus");
			if ("1".equals(rechargeStatus)) {
				orderBean.setStatus("7");
				orderBean.setMessage("充值成功");
				orderBean.setErrorMsg("充值成功");
		
			}else if ("2".equals(rechargeStatus)) {
				orderBean.setStatus("1");
				orderBean.setMessage("充值中");
				orderBean.setErrorMsg("充值中");
				
			}else {
				String failcode = res.getString("failcode");
				orderBean.setStatus("8");
				orderBean.setMessage("["+failcode+"]"+parseStatusCodeToMsg(failcode));
				orderBean.setErrorMsg("["+failcode+"]"+parseStatusCodeToMsg(failcode));
			} 

		} else {
			orderBean.setStatus("1");
			orderBean.setMessage("充值中");
			orderBean.setErrorMsg("充值中");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
	
	public String parseResultCodeToMsg(String code) {
		String msg = "";
		if ("200".equals(code))
			msg = "全部成功";
		else if ("301".equals(code))
			msg = "手机号码错误";
		else if ("303".equals(code))
			msg = "手机号与充值商品运营商类型或者所属地域不匹配 ";
		else if ("304".equals(code))
			msg = "输入参数错误";
		else if ("305".equals(code))
			msg = "加密错误";
		else if ("306".equals(code))
			msg = "目的地为空";
		else if ("307".equals(code))
			msg = "用户是锁定状态";
		else if ("308".equals(code))
			msg = "用户格式不支持";
		else if ("309".equals(code))
			msg = "用户地址不在信任列表";
		else if ("311".equals(code))
			msg = "商品不存在或已下架";
		else if ("312".equals(code))
			msg = "用户秘钥存在问题";
		else if ("313".equals(code))
			msg = "用户未开通接口 ";
		else if ("314".equals(code))
			msg = "商品库存不足";
		else if ("318".equals(code))
			msg = "账户余额不足";
		else if ("324".equals(code))
			msg = "价格异常";
		else if ("325".equals(code))
			msg = "要查询的信息未找到";
		else if ("326".equals(code))
			msg = "库存中的包，充值时间已失效";
		else if ("327".equals(code))
			msg = "用户IP未绑定";
		else if ("333".equals(code))
			msg = "一天内充值失败3次以上";
		else if ("334".equals(code))
			msg = "充值手机号为平台黑名单";
		else if ("336".equals(code))
			msg = "订单重复提交";
		else if ("888".equals(code))
			msg = "订购失败，未知异常";
		else if ("999".equals(code))
			msg = "系统未知异常，联系管理员";
		else {
			msg = "other fail reason";
		}
		return msg;
	}
	
	public String parseStatusCodeToMsg(String code) {
		String msg = "";
		if ("000".equals(code))
			msg = "订购成功";
		else if ("002".equals(code))
			msg = "运营商boss系统异常";
		else if ("003".equals(code))
			msg = "运营商解析服务异常 ";
		else if ("004".equals(code))
			msg = "此地区产品未开通";
		else if ("005".equals(code))
			msg = "订单请求重复提交";
		else if ("006".equals(code))
			msg = "运营商查无此用户";
		else if ("007".equals(code))
			msg = "用户已停机";
		else if ("009".equals(code))
			msg = "手机号为运营商黑明单";
		else if ("010".equals(code))
			msg = "与已订购的其他产品冲突";
		else if ("011".equals(code))
			msg = "运营商内部异常未知原因";
		else if ("012".equals(code))
			msg = "运营商原因订购失败";
		else if ("013".equals(code))
			msg = "该地区暂不支持2G用户订购 ";
		else if ("014".equals(code))
			msg = "用户当月流量包已到达叠加上限";
		else if ("015".equals(code))
			msg = "暂时无产品订购，针对预付用户";
		else if ("016".equals(code))
			msg = "2G/3G 融合用户不允许订购";
		else if ("017".equals(code))
			msg = "用户状态异常";
		else if ("018".equals(code))
			msg = "用户主套餐变更，当月不能订购";
		else if ("019".equals(code))
			msg = "用户有正在处理订单";
		else if ("020".equals(code))
			msg = "该地区暂不支持 4G 用户订购";
		else if ("021".equals(code))
			msg = "该地区暂不支持 3G 用户订购";
		else if ("022".equals(code))
			msg = "用户网别变更，不允许订购";
		else if ("023".equals(code))
			msg = "产品不允许重复订购";
		else if ("024".equals(code))
			msg = "运营商省份系统异常";
		else if ("025".equals(code))
			msg = "省份月末初进出帐无法订购";
		else if ("026".equals(code))
			msg = "省份系统超时,请稍后再试";
		else if ("027".equals(code))
			msg = "手机用户可用额度不足";
		else if ("028".equals(code))
			msg = "用户帐户异常";
		else if ("031".equals(code))
			msg = "运营商系统升级中无法订购";
		else if ("032".equals(code))
			msg = "用户手机已欠费";
		else if ("033".equals(code))
			msg = "用户手机号码已冻结或注销";
		else if ("034".equals(code))
			msg = "用户在运营商系统有在途工单，无法受理";
		else if ("035".equals(code))
			msg = "手机号能够订购指定销售品";
		else if ("036".equals(code))
			msg = "手机号不能订购指定销售品";
		else if ("024".equals(code))
			msg = "运营商省份系统异常";
		else if ("041".equals(code))
			msg = "客户业务受限";
		else if ("042".equals(code))
			msg = "运营商网络异常";
		else if ("043".equals(code))
			msg = "异常订单 人工处理为失败";
		else if ("044".equals(code))
			msg = "用户信息不存在";
		else if ("045".equals(code))
			msg = "运营商地区维护 暂不能充值";
		else if ("046".equals(code))
			msg = "运营商限制客户无法订购";
		else if ("047".equals(code))
			msg = "运营商限制客户无法订购";
		else if ("048".equals(code))
			msg = "暂时不支持此类手机号的充值";
		else if ("049".equals(code))
			msg = "用户非指定订购的有效成员";
		else if ("050".equals(code))
			msg = "用户套餐枚举值错误";
		else if ("051".equals(code))
			msg = "省公司开通叠加包失败";
		else if ("052".equals(code))
			msg = "用户状态不正确或者用户基础通信能力不正确";
		else if ("053".equals(code))
			msg = "用户已订购其他互斥业务";
		else if ("054".equals(code))
			msg = "用户叠加包套餐ID错误";
		else if ("056".equals(code))
			msg = "运营商系统故障，请稍候再试！";
		else if ("057".equals(code))
			msg = "订购失败";
		else if ("058".equals(code))
			msg = "该商品不支持用户购买";
		else if ("059".equals(code))
			msg = "该用户叠加订购该产品已超";
		else {
			msg = "other fail reason";
		}
		return msg;
	}
}
